/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.WarningMessage;
import me.zhengjie.modules.system.service.WarningMessageService;
import me.zhengjie.modules.system.service.dto.WarningMessageQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.system.service.dto.WarningMessageDto;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @author 李忠臻
* @date 2024-02-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "告警信息管理")
@RequestMapping("/api/warningMessage")
public class WarningMessageController {

    private final WarningMessageService warningMessageService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('warningMessage:list')")
    public void exportWarningMessage(HttpServletResponse response, WarningMessageQueryCriteria criteria) throws IOException {
        warningMessageService.download(warningMessageService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询告警信息")
    @ApiOperation("查询告警信息")
//    @PreAuthorize("@el.check('warningMessage:list')")
    public ResponseEntity<PageResult<WarningMessageDto>> queryWarningMessage(WarningMessageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(warningMessageService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增告警信息")
    @ApiOperation("新增告警信息")
    @PreAuthorize("@el.check('warningMessage:add')")
    public ResponseEntity<Object> createWarningMessage(@Validated @RequestBody WarningMessage resources){
        warningMessageService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改告警信息")
    @ApiOperation("修改告警信息")
    @PreAuthorize("@el.check('warningMessage:edit')")
    public ResponseEntity<Object> updateWarningMessage(@Validated @RequestBody WarningMessage resources){
        warningMessageService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除告警信息")
    @ApiOperation("删除告警信息")
    @PreAuthorize("@el.check('warningMessage:del')")
    public ResponseEntity<Object> deleteWarningMessage(@RequestBody Long[] ids) {
        warningMessageService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping(value = "/deleteAll")
    @Log("清空告警信息")
    @ApiOperation("清空告警信息")
    @PreAuthorize("@el.check('warningMessage:del')")
    public ResponseEntity<Object> deleteAllWarningMessage() {
        warningMessageService.deleteAll(null);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/upload")
    @ApiOperation("上传文件")
//    @PreAuthorize("@el.check('purchaseRequest:add')")
    public ResponseEntity<Object> uploadPurchaseRequest(@RequestParam String name, @RequestParam("file") MultipartFile file) {
//        localStorageService.create(name, file);
//        PurchaseRequest resources = new PurchaseRequest();
        warningMessageService.upload(name, file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}